<?php

class Core_Frontend_Module_Concessions_Latest extends Dcmp_Module_Base {

    protected $name = 'concessions_latest';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form) {
        $categories = App::getModel('promotion/collection/Concessions_category')->load();
        $form->addComboTreeField($this->__('article.label.category'), 'category', "module[parameters][category]")
                ->bindData($categories, 0, array('valueKey'=>'id', 'textKey'=>'title', 'parentKey'=>'parent_id', 'childKey'=>'id'))
                ->setDefaultOption('all',$this->__('share.label.all'));
        $form->addSelectField($this->__('article.label.condition'),'sort_condition','module[parameters][sort_condition]')
                ->setOption($this->__('article.label.clickcount'),'click_count')
                ->setOption($this->__('article.label.modified_time'),'modified_time')
                ->setOption($this->__('article.label.hot'),'hot')
                ->setOption($this->__('article.label.new'),'new')
                ->setOption($this->__('article.label.top'),'top');
        $form->addTextBoxField($this->__('share.label.item_count'),'item_count','module[parameters][item_count]')
             ->addRequiredRule()
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_count'),'image_count','module[parameters][image_count]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_count'),'introduction_count','module[parameters][introduction_count]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_size'),'introduction_size','module[parameters][introduction_size]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_width'),'image_width','module[parameters][image_width]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_height'),'image_height','module[parameters][image_height]')
             ->setValue(0)
             ->addDigitsRule();
        $form->createTab('module_option', $this->__('design.label.module_parameter'),
                array('field_category', 'field_sort_condition', 'field_item_count', 'field_image_count', 'field_introduction_count', 'field_introduction_size',
                    'field_image_width', 'field_image_height'));
    }

    public function render() {
        $concessions = App::getModel('promotion/collection_concessions')
                        ->resetSelect()->field('a.name')->field('is_new')->field('picture')->field('a.id')->field('is_hot')
                        ->filterByStatus('published')
                        ->filterByStartTime(date('Y-m-d H:m:s'))
                        ->filterByEndTime(date('Y-m-d H:m:s'))
                        ->sortBy($this->getValueByKey('sort_condition'),'desc')
                        ->limit($this->getValueByKey('item_count'),0);
        if($this->getValueByKey('category')=="all") $concessions->load();
        else $concessions->filterByCategoryId($this->getValueByKey('category'))->load();
        $nameStartIndex = 0;
        $obj = new Kd_Object($this->getParameters());
        if($obj->getImageCount()){
            $obj->setIsShowImage(TRUE);
            $obj->setEndImageIndex(min(count($concessions), $obj->getImageCount()));
            $nameStartIndex = max($obj->getEndImageIndex(), $nameStartIndex);
        }
        if($obj->getIntroductionCount()){
            $obj->setIsShowIntroduction(TRUE);
            $obj->setEndIntroductionIndex(min(count($concessions), $obj->getIntroductionCount()));
            $nameStartIndex = max($obj->getEndIntroductionIndex(), $nameStartIndex);
        }
        $obj->setEndNameIndex(min(count($concessions), $obj->getItemCount()));
        $tmpl = $this->getTemplate()->assignMulti(array('concessions' => $concessions, 'parameters' => $obj));
        return $tmpl->render($this->getTemplatePath($obj->getTemplateName(), $obj->getTemplateCode()));
    }

}

